<?php
namespace Human\Model;
use Think\Model;
/**
 * 员工操作
 */
class EmployeeModel extends Model {

    //完成
    protected $_auto = array (

    );

    //验证
    protected $_validate = array(
        array('employee_name','require', '请填写员工名称'),
        array('employee_real_name','require', '请填写员工真实姓名'),
        array('employee_mobile','require', '请填写员工手机'),
        array('employee_email','require', '请填写员工邮箱'),
        array('store_id','require', '请选择店铺'),
        array('department_id','require', '请选择部门'),
        array('job_id','require', '请选择职务'),
    );

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array(), $limit = 0){
        $data   = $this->table("__EMPLOYEE__ as A")
                    ->join('__DEPARTMENT__ as C ON A.department_id = C.department_id')
                    ->join('__JOB__ as D ON A.job_id = D.job_id')
                    ->join('__STORE__ as E ON E.store_id = A.store_id')
                    ->field('A.*,C.department_name,D.job_name,E.store_name')
                    ->where($where)
                    ->limit($limit)
                    ->order('A.employee_id')
                    ->select();
        return $data;
    }

    /**
     * 获取列表
     * @return array 列表
     */
    public function countList($where = array()){
        return  $this->table("__EMPLOYEE__ as A")
                    ->join('__DEPARTMENT__ as C ON A.department_id = C.department_id')
                    ->join('__JOB__ as D ON A.job_id = D.job_id')
                    ->field('A.*,C.department_name,D.job_name')
                    ->where($where)
                    ->count();
    }

    /**
     * 获取信息
     * @param int $employeeId ID
     * @return array 信息
     */
    public function getInfo($employeeId)
    {
    	return $this->where('employee_id ='.$employeeId)->find();
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {
    	return $this->where($where)->find();
    }

    /**
     * 更新信息
     * @param string $type 更新类型
     * @return bool 更新状态
     */
    public function saveData($type = 'add'){
        
        //事务处理
        $this->startTrans();
        $data = $this->create();
        if(!$data){
            $this->rollback();
            return false;
        }
        if($type == 'add'){
            //写入数据
            $data['employee_create_time']=time();
            $data['operator_id']	= session('admin_user.user_id');
            $data['operator_name']	= session('admin_user.user_name');
            $status = $this->add($data);
            if($status){
                $this->commit();
            }else{
                $this->rollback();
            }
            return $status;
        }
        if($type == 'edit'){
            //修改数据
            $status = $this->where('employee_id='.$data['employee_id'])->save();
            if($status === false){
                $this->rollback();
                return false;
            }
            $this->commit();
            return true;
        }
        $this->rollback();
        return false;
    }

    /**
     * 删除信息
     * @param int $employeeId ID
     * @return bool 删除状态
     */
    public function delData($employeeId)
    {
        $map = array();
        $map['employee_id'] = $employeeId;
        return $this->where($map)->delete();
    }

    /**
     *更新字段信息
     *@param array $data filed
     *@param array $$where 条件
     *@return bool 更新状态
     *
     */
    public function updateData($data,$where)
    {
        return $this->where($where)->save($data);
    }
}
